const { defineConfig } = require("@vue/cli-service");
const path = require("path");
const PrerenderSPAPlugin = require("./build/prerender-spa-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const outputDir = path.resolve(__dirname, process.env.VUE_APP_OUTDIR || "dist");

// const useCopy = () => new CopyPlugin({
//     patterns: [
//         { from: path.resolve(__dirname, '../resource'), to: outputDir },
//     ],
// })

const routes = ["/about/"];

const usePrerender = () =>
  new PrerenderSPAPlugin({
    staticDir: outputDir,
    routes,
  });
module.exports = defineConfig({
  transpileDependencies: true,
  // @FIXME 这里不能设定，因为 prerender-spa-plugin 打包会失败
  publicPath:
    /* process.env.NODE_ENV === 'production' ? process.env.VUE_APP_BASE_URL : */ "/",
  outputDir,
  assetsDir: "assets",
  productionSourceMap: false,
  filenameHashing: true,
  lintOnSave: true,

  devServer: {
    port: 5757,
    https: false,
  },

  css: {
    loaderOptions: {
      less: {},
    },
  },

  configureWebpack(config) {
    config.resolve.alias["@"] = path.join(__dirname, "src");
    // config.plugins.push(useCopy())

    if (process.env.NODE_ENV === "production") {
      // config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
      config.plugins.push(usePrerender()); // 打包的时候使用预解析成 html
    }
  },

  chainWebpack(config) {
    // 移除prefetch和preload
    config.plugins.delete("prefetch");
    config.plugins.delete("preload");
  },
});
